/*!
 *  TinyAjax v1.0.0
 *  Copyright 2018, Frank Chao
 *  Released under the MIT license
 */
!(function ( global, factory ) {

	if ( typeof define === "function" && define.amd ) {
		define(factory);
	} else if ( typeof module !== "undefined" && typeof exports === "object" ) {
		module.exports = factory();
	} else {
		global.Ajax = factory();
	}

})( typeof window !== "undefined" ? window : this, function () {

	function random () {
		return "ID" + parseInt(Math.random() * 1e+14);
	}
	function request ( type, url, data, callback ) {
		var $url = url, 
			$data, 
			$callback;
		if ( callback ) {
			$data = data;
			$callback = callback;
		} else {
			if ( typeof data === "function" ) {
				$callback = data;
			} else {
				$data = data;
			}
		}

		var param = "";
		if ( $data ) {
			param = typeof $data === "string" ? $data : (function () {
				var set = [];
				for ( var i in $data ) {
					set.push(encodeURIComponent(i) + "=" + encodeURIComponent($data[i]));
				}
				return set.join("&");
			})();
		}

		if ( type === "GET" && param ) {
			$url = $url + ($url.indexOf("?") < 0 ? "?" : "&") + param;
		}

		var xmlHttp = new XMLHttpRequest();
		xmlHttp.onreadystatechange = function () {
			if ( xmlHttp.readyState === 4 ) {
				try {
					var status = xmlHttp.status;
					if ( (status >= 200 && status < 300) || status === 304  ) {
						var response = xmlHttp.responseText.replace(/(^\s*)|(\s*$)/g, "");  
						$callback && $callback(response, status, xmlHttp);
					}
				} catch ( e ) {}
			}
		}
		xmlHttp.open(type, $url, true);
		if ( type === "POST" ) {
	        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
	       	xmlHttp.send(param);
	    } else {
	        xmlHttp.send(null);
	    }
	}

	var Ajax = {
		get: function ( url, data, callback ) {
			request("GET", url, data, callback);
		},
		post: function ( url, data, callback ) {
			request("POST", url, data, callback);
		},
		jsonp: function ( url, name, _callback ) {
			var callback = _callback || name;
			var id = random();
			var fnName = "";
			if ( _callback ) {
				url = url + "&callback=" + name;
			}
			fnName = _callback ? name : url.substring(url.lastIndexOf("callback=") + 9);
			if ( url[(url.length - 1)] === "?" ) {  
				fnName = id;
				url = url.replace(/(\?$)/g, id);
			}
			var cache = window[fnName];
			var data;
			window[fnName] = function ( jsonpData ) {
				data = jsonpData;
			}

			var script = document.createElement("script");
            script.src = url;
            script.id = id;
            document.getElementsByTagName("head")[0].appendChild(script);

            function recovery () {
                var ajaxScript = document.getElementById(id);
                ajaxScript.parentNode.removeChild(ajaxScript);
				script = null;
				window[fnName] = cache;
            }

            function success () {
            	callback(data);
                recovery();
            }      
                
            if ( !Array.isArray ) {
                script.onreadystatechange = function () {
                    if ( /loaded|complete/.test(script.readyState) ) { 
                        script.onreadystatechange = null;
                        success();
                    }
                }
            } else {
            	script.onload = function () {
	                script.onload = null;
	                success();
	            }
            }
		}
	};
	return Ajax;

});